﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VisualPoint.Controls.Actions;

namespace VisualPoint.Controls.Models
{
    public class ActionListModel : BaseModel
    {
        public ActionListModel()
        {
            _actions = new BaseActionCollection();
        }

        private BaseActionCollection _actions;
        public BaseActionCollection Actions
        {
            get
            {
                return _actions;
            }
            set
            {
                _actions = value;
                DoPropertyChanged("Actions");
            }
        }

        private BaseActionModel _SelectedItem;
        public BaseActionModel SelectedItem
        {
            get
            {
                return _SelectedItem;
            }
            set
            {
                if (_SelectedItem != value)
                {
                    _SelectedItem = value;
                    DoPropertyChanged("SelectedItem");
                }
            }
        }

        public int SelectedItemIndex
        {
            get
            {
                if (_SelectedItem == null)
                {
                    return -1;
                }
                return Actions.IndexOf(_SelectedItem);
            }
        }

        private string _message;

        public string Message
        {
            get { return _message; }
            set 
            {
                if (_message != value)
                {
                    _message = value;
                    DoPropertyChanged("Message");
                }
            
            }
        }
        

        public void AddAction(BaseActionModel action)
        {
            bool? status = action.AddItem();
            if (status.HasValue && status.Value)
            {
                Actions.Insert(SelectedItemIndex + 1, action);
                //Actions.Add(action);
            }
        }

        public string GetSlideActions()
        {
            SlideActions slides = new SlideActions();
            foreach (BaseActionModel action in Actions)
            {
                slides.Actions.Add(action.GetAction());
            }
            return slides.Serialize();
        }

        public void SetSlideActions(string data, bool add)
        {
            if (!add)
            {
                Actions = new BaseActionCollection();
            }
            if (!string.IsNullOrEmpty(data))
            {
                SlideActions slides = SlideActions.DeSerialize(data);
                foreach (BaseAction action in slides.Actions)
                {
                    BaseActionModel model = CreateActionModel(action);
                    if (model != null)
                    {
                        _actions.Add(model);
                    }
                }
            }
        }

        private BaseActionModel CreateActionModel(BaseAction action)
        {
            return action.CreateModel();
        }

        public void MoveItemUp()
        {
            if (SelectedItem != null)
            {
                int i = Actions.IndexOf(SelectedItem);
                if (i > 0)
                {
                    Actions.Move(i, i - 1);
                }
            }
        }

        public void MoveItemDown()
        {
            if (SelectedItem != null)
            {
                int i = Actions.IndexOf(SelectedItem);
                if (i < (Actions.Count - 1))
                {
                    Actions.Move(i, i + 1);
                }
            }
        }


    }
}
